package edu.ped.android.nfc;

import java.io.IOException;

import edu.ped.android.utils.NFCUtil;
import edu.ped.android.utils.PayloadTag;
import edu.ped.android.utils.TagsMatrice;
import edu.ped.android.utils.VibratorManager;
import edu.ped.android.view.MatriceView;
import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem;
import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.TableLayout;

public class ShowPictureActivity extends GDActivity{

	int cpt=0;
	// Progress bar
	public static final int MSG_START = 2;

	TableLayout m_tab;
	NFCUtil nfc;
	TagsMatrice tm;
	Context mContext;
	MatriceView matriceView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setActionBarContentView(R.layout.activity_show_draw);
		/*Title Bar*/
		ActionBarManager.initActionBar(this,ActionBarManager.SHOWPICTURE);
		
		mContext = this;
		matriceView = (MatriceView) findViewById(R.id.matriceView);
	
	}
	
	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		 if(!ActionBarManager.handleActionBar(this, item, position))
			 return super.onHandleActionBarItemClick(item, position);
		 return true;
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())
				|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent
						.getAction())
						|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent
								.getAction())) {
			/* Object */
			try {
				PayloadTag current = null;
				try {
					current = NFCUtil.readTagObject(intent);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(current !=null){
					if(tm == null){
						tm= new TagsMatrice(current.getMatriceHeight(), current.getMatriceWidth());
						matriceView.setMatrice(tm);
					}
					tm.setTag(current.getX(),current.getY(),current.getMatriceHeight(),current.getMatriceWidth(),current.getValeur());
					
					VibratorManager.getVibrator().vibrate(50);
					matriceView.draw=true;
					matriceView.invalidate();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		NFCUtil.getNFCFocus(this);
	}
}
